package cn.easier.activity.console.controller;

import cn.easier.activity.console.service.ActivityRecordStatisticsService;
import cn.easier.activity.console.vo.CouponCodeDetailsVo;
import cn.easier.activity.web.entity.CouponType;
import cn.easier.activity.web.entity.ExchangeState;
import cn.easier.activity.web.entity.PrizeType;
import cn.easier.club.base.dto.ResultInfo;
import com.google.common.base.Strings;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 活动记录统计 Controller
 */
@Controller
@RequestMapping("/activity-record/statistics")
public class ActivityRecordStatisticsController {
    Logger logger = LoggerFactory.getLogger(ActivityRecordStatisticsController.class);

    @Autowired
    ActivityRecordStatisticsService activityRecordStatisticsService;

    /**
     * 分页获取用户券码列表
     *
     * @param phoneNum  用户
     * @param pageIndex 页码
     * @return
     */
    @GetMapping("/coupon-code/Details")
    @ResponseBody
    public ResultInfo findCouponCodeDetails(@RequestParam(value = "phoneNum") String phoneNum, @RequestParam(value = "pageIndex", required = false, defaultValue = "1") int pageIndex) {
        //简单的参数校验
        if (Strings.isNullOrEmpty(phoneNum) || phoneNum.length() < 10 || phoneNum.length() > 20 || pageIndex < 0) {
            return new ResultInfo(String.valueOf(HttpServletResponse.SC_INTERNAL_SERVER_ERROR), "参数不合法", null);
        }
        int pageNum = 10; //每页条目
        ResultInfo resultInfo = null;
        try {
            logger.info("活动记录统计，用户券码详情。phoneNum：=>{}", phoneNum);
            int couponCodeDetailsCount = activityRecordStatisticsService.countActivityCouponCodeRecordByPhoneNum(phoneNum);
            logger.info("活动记录统计，用户券码总量。couponCodeDetailsCount：=>{}", couponCodeDetailsCount);
            if (couponCodeDetailsCount < 1) {
                return new ResultInfo(String.valueOf(HttpServletResponse.SC_INTERNAL_SERVER_ERROR), "没有用户券码记录", null);
            }
            List<CouponCodeDetailsVo> couponCodeDetailsVoList = new ArrayList<>();
            Object[] couponCodeDetails = activityRecordStatisticsService.findActivityCouponCodeRecordsByPhoneNum(phoneNum, (pageIndex - 1) * pageNum, pageNum);

            //Object 转 CouponCodeDetailsVo
            for (Object couponCodeDetail : couponCodeDetails) {
                Object[] details = (Object[]) couponCodeDetail;
                CouponCodeDetailsVo couponCodeDetailsVo = new CouponCodeDetailsVo();
                couponCodeDetailsVo.setPhoneNum((String) details[0]);
                couponCodeDetailsVo.setCreateDate((Date) details[1]);
                couponCodeDetailsVo.setActivityName((String) details[2]);
                couponCodeDetailsVo.setPrizeName((String) details[3]);
                Integer prizeTypeIndex = (Integer) details[4];
                if (prizeTypeIndex != null) {
                    String prizeTypeName = PrizeType.values()[prizeTypeIndex].getTypeName();
                    couponCodeDetailsVo.setPrizeTypeName(prizeTypeName);
                }

                couponCodeDetailsVo.setAwarded((Boolean) details[5]);
                Integer exchangeStateIndex = (Integer) details[6];
                if (exchangeStateIndex != null) {
                    String exchangeStateName = ExchangeState.values()[exchangeStateIndex].getState();
                    couponCodeDetailsVo.setExchangeStateName(exchangeStateName);
                }
                couponCodeDetailsVo.setBill((Integer) details[7]);
                couponCodeDetailsVo.setUrl((String) details[8]);
                Integer couponTypeIndex = (Integer) details[9];
                if (couponTypeIndex != null) {
                    String couponTypeName = CouponType.values()[couponTypeIndex].getTypeName();
                    couponCodeDetailsVo.setCouponTypeName(couponTypeName);
                }
                couponCodeDetailsVo.setCode((String) details[10]);
                couponCodeDetailsVo.setSecretKey((String) details[11]);

                //添加用户券码详情至列表
                couponCodeDetailsVoList.add(couponCodeDetailsVo);
            }

            logger.info("活动记录统计，用户券码列表。pageIndex：=>{}, couponCodeDetailsVos size: ={}", pageIndex, couponCodeDetailsVoList);
            resultInfo = new ResultInfo(String.valueOf(HttpServletResponse.SC_OK), String.valueOf(couponCodeDetailsCount), couponCodeDetailsVoList);
        } catch (Exception e) {
            e.printStackTrace();
            logger.info("获取用户券码详情异常，phoneNum：=>{}", phoneNum);
        }
        return resultInfo;
    }
}
